package com.ruoyi.web.controller.honor;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.honor.domain.SysHonorReward;
import com.ruoyi.honor.domain.SysHonorMember;
import com.ruoyi.honor.service.ISysHonorRewardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 荣誉奖励Controller
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@RestController
@RequestMapping("/honor/reward")
public class SysHonorRewardController extends BaseController
{
    @Autowired
    private ISysHonorRewardService sysHonorRewardService;

    /**
     * 查询荣誉奖励列表
     */
    @PreAuthorize("@ss.hasPermi('honor:reward:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysHonorReward sysHonorReward)
    {
        startPage();
        List<SysHonorReward> list = sysHonorRewardService.selectSysHonorRewardList(sysHonorReward);
        return getDataTable(list);
    }

    /**
     * 导出荣誉奖励列表
     */
    @PreAuthorize("@ss.hasPermi('honor:reward:export')")
    @Log(title = "荣誉奖励", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysHonorReward sysHonorReward)
    {
        List<SysHonorReward> list = sysHonorRewardService.selectSysHonorRewardList(sysHonorReward);
        ExcelUtil<SysHonorReward> util = new ExcelUtil<SysHonorReward>(SysHonorReward.class);
        util.exportExcel(response, list, "荣誉奖励数据");
    }

    /**
     * 获取荣誉奖励详细信息
     */
    @PreAuthorize("@ss.hasPermi('honor:reward:query')")
    @GetMapping(value = "/{honorId}")
    public AjaxResult getInfo(@PathVariable("honorId") Long honorId)
    {
        SysHonorReward honorReward = sysHonorRewardService.selectSysHonorRewardByHonorId(honorId);
        List<SysHonorMember> members = sysHonorRewardService.selectMembersByHonorId(honorId);
        honorReward.setRemark(members.toString()); // 临时存储关联人员信息
        return success(honorReward);
    }

    /**
     * 新增荣誉奖励
     */
    @PreAuthorize("@ss.hasPermi('honor:reward:add')")
    @Log(title = "荣誉奖励", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysHonorReward sysHonorReward)
    {
        sysHonorReward.setCreateBy(getUsername());
        return toAjax(sysHonorRewardService.insertSysHonorReward(sysHonorReward));
    }

    /**
     * 修改荣誉奖励
     */
    @PreAuthorize("@ss.hasPermi('honor:reward:edit')")
    @Log(title = "荣誉奖励", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysHonorReward sysHonorReward)
    {
        sysHonorReward.setUpdateBy(getUsername());
        return toAjax(sysHonorRewardService.updateSysHonorReward(sysHonorReward));
    }

    /**
     * 删除荣誉奖励
     */
    @PreAuthorize("@ss.hasPermi('honor:reward:remove')")
    @Log(title = "荣誉奖励", businessType = BusinessType.DELETE)
	@DeleteMapping("/{honorIds}")
    public AjaxResult remove(@PathVariable Long[] honorIds)
    {
        return toAjax(sysHonorRewardService.deleteSysHonorRewardByHonorIds(honorIds));
    }

    /**
     * 保存荣誉奖励及关联人员
     */
    @PreAuthorize("@ss.hasPermi('honor:reward:edit')")
    @Log(title = "荣誉奖励", businessType = BusinessType.UPDATE)
    @PostMapping("/saveWithMembers")
    public AjaxResult saveWithMembers(@RequestBody SysHonorReward sysHonorReward)
    {
        sysHonorReward.setCreateBy(getUsername());
        sysHonorReward.setUpdateBy(getUsername());
        // 这里需要从前端传递关联人员ID数组，暂时使用空数组
        Long[] membersIds = new Long[0];
        return toAjax(sysHonorRewardService.saveHonorRewardWithMembers(sysHonorReward, membersIds));
    }

    /**
     * 根据年份统计荣誉数量
     */
    @GetMapping("/countByYear/{year}")
    public AjaxResult countByYear(@PathVariable String year)
    {
        int count = sysHonorRewardService.countHonorByYear(year);
        return success(count);
    }

    /**
     * 根据级别统计荣誉数量
     */
    @GetMapping("/countByLevel/{level}")
    public AjaxResult countByLevel(@PathVariable String level)
    {
        int count = sysHonorRewardService.countHonorByLevel(level);
        return success(count);
    }

    /**
     * 获取荣誉奖励关联人员列表
     */
    @GetMapping("/members/{honorId}")
    public AjaxResult getMembersByHonorId(@PathVariable Long honorId)
    {
        List<SysHonorMember> members = sysHonorRewardService.selectMembersByHonorId(honorId);
        return success(members);
    }
}
